<?php

namespace app\storeapi\model;
use app\common\model\Gifts as GiftsModel;
/**
 * 商品模型
 * Class Goods
 * @package app\store\model
 */
class Gifts extends GiftsModel
{
    const FORM_SCENE_ADD = 'add';
    const FORM_SCENE_EDIT = 'edit';


    /**
     * 修改商品状态
     * @param $gifts_status
     * @return false|int
     */
    public function setStatus($gifts_status)
    {
        return $this->allowField(true)->save(['gifts_status' => $gifts_status]) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->allowField(true)->save(['is_delete' => 1]);
    }


    /**
     * 添加商品
     * @param array $data
     * @return bool
     * @throws \think\exception\PDOException
     */
    public function add(array $data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_ADD)) {
            return false;
        }
        $data['wxapp_id'] =  self::$wxapp_id;
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        // 开启事务
        $this->startTrans();
        try {
            // 添加商品
            $this->allowField(true)->save($data);
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }

    /**
     * 编辑商品
     * @param $data
     * @return bool|mixed
     */
    public function edit($data)
    {
        // 表单验证
        if (!$this->validateForm($data, self::FORM_SCENE_EDIT)) {
            return false;
        }

        $data['wxapp_id'] =  self::$wxapp_id;
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        return $this->transaction(function () use ($data) {
            // 保存商品
            $this->allowField(true)->save($data);
            return true;
        });
    }

    /**
     * 表单验证
     * @param $data
     * @param string $scene
     * @return bool
     * @throws \think\exception\DbException
     */
    private function validateForm($data, $scene = self::FORM_SCENE_ADD)
    {
        if(!isset($data['linkman']) || empty($data['linkman'])){
            $this->error = '请输入联系人';
            return false;
        }
        if(!isset($data['phone']) || empty($data['phone'])){
            $this->error = '请输入联系电话';
            return false;
        }
        if(!isset($data['address']) || empty($data['address'])){
            $this->error = '请输入地址';
            return false;
        }

        if (empty($data['image_id'])) {
            $this->error = '请上传图片';
            return false;
        }
        if (!trim($data['gifts_title'])) {
            $this->error = '请输入礼包名称';
            return false;
        }

        if (!trim($data['gifts_content'])) {
            $this->error = '请输入礼包简介';
            return false;
        }
        if ($data['gifts_type']==20) {
            if(empty($data['gifts_price'])){
                $this->error = '请输入礼包现价';
                return false;
            }
        }
        if(empty($data['gifts_stock_num'])){
            $this->error = '请输入礼包库存';
            return false;
        }
        if(empty($data['start_time'])){
            $this->error = '请输入开始时间';
            return false;
        }
        if(empty($data['end_time'])){
            $this->error = '请输入结束时间';
            return false;
        }

        return true;
    }








}
